//
//  RHDeviceBatteryCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/26.
//  电池 --- 能量块

#import "RHDeviceBatteryCell.h"
@interface RHDeviceBatteryCell()
@property (nonatomic, strong) QMUILabel *statusContentLab;

@property (nonatomic, strong) QMUILabel *powerContentLab;

@property (nonatomic, strong) QMUILabel *socContentLab;

@property (nonatomic, strong) UIProgressView *socProgress;
@end
@implementation RHDeviceBatteryCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setBlockModel:(RHEnergyBlockModel *)blockModel
{
    _blockModel = blockModel;
    self.statusContentLab.text = blockModel.status == 0? @"Normal":@"Stop";
    self.powerContentLab.text = [NSString stringWithFormat:@"%ld%%",blockModel.power];
    self.socContentLab.text = [NSString stringWithFormat:@"%ld%%",blockModel.soc];
    self.socProgress.progress = blockModel.soc/100.0;
}


-(void)setupUI
{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 15, 0, 15));
    }];
    
    UIImageView *blockImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"block_icon"]];
    [bgView addSubview:blockImg];
    
    QMUILabel *blockTitleLab = [[QMUILabel alloc]init];
    blockTitleLab.textAlignment = NSTextAlignmentCenter;
    blockTitleLab.text = @"1#block";
    blockTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    blockTitleLab.font = kSYSTEMFONT(15);
    [bgView addSubview:blockTitleLab];
    
    QMUIButton *detailBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [detailBtn setImage:[UIImage imageNamed:@"block_detail"] forState:UIControlStateNormal];
    [bgView addSubview:detailBtn];
    [[detailBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.blockDetailBlock){
            weakSelf.blockDetailBlock();
        }
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"E7E7E7");
    [bgView addSubview:line];
  
    //工作状态
    QMUILabel *statusTitleLab = [[QMUILabel alloc]init];
    statusTitleLab.textAlignment = NSTextAlignmentLeft;
    statusTitleLab.text = [NSString stringWithFormat:@"%@:",@"工作状态".rh_localized];
    statusTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    statusTitleLab.font = kSYSTEMFONT(14);;
    [bgView addSubview:statusTitleLab];
    
    QMUILabel *statusContentLab = [[QMUILabel alloc]init];
    statusContentLab.textAlignment = NSTextAlignmentRight;
    statusContentLab.text = @"stop";
    statusContentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    statusContentLab.font = kSYSTEMFONT(14);;
    [bgView addSubview:statusContentLab];
    self.statusContentLab = statusContentLab;
    
    //实时功率
    QMUILabel *powerTitleLab = [[QMUILabel alloc]init];
    powerTitleLab.textAlignment = NSTextAlignmentLeft;
    powerTitleLab.text = [NSString stringWithFormat:@"%@:",@"实时功率".rh_localized];;
    powerTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    powerTitleLab.font = kSYSTEMFONT(14);;
    [bgView addSubview:powerTitleLab];
    
    UIImageView *powerImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"block_power_up"]];
    [bgView addSubview:powerImg];
    
    QMUILabel *powerContentLab = [[QMUILabel alloc]init];
    powerContentLab.textAlignment = NSTextAlignmentRight;
    powerContentLab.text = @"14Kw";
    powerContentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    powerContentLab.font = kBOLDSYSTEMFONT(14);
    [bgView addSubview:powerContentLab];
    self.powerContentLab = powerContentLab;
    
    //SOC
    QMUILabel *socTitleLab = [[QMUILabel alloc]init];
    socTitleLab.textAlignment = NSTextAlignmentLeft;
    socTitleLab.text = @"SOC:";
    socTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    socTitleLab.font = kSYSTEMFONT(14);;
    [bgView addSubview:socTitleLab];
    
    UIProgressView *socProgress = [[UIProgressView alloc]init];
    socProgress.progress = 0.8;
    socProgress.progressTintColor = kCOLOR_HEX(@"678EF2");
    socProgress.trackTintColor = kCOLOR_HEX(@"E6E6E6");
    [bgView addSubview:socProgress];
    self.socProgress = socProgress;
    
    QMUILabel *socContentLab = [[QMUILabel alloc]init];
    socContentLab.textAlignment = NSTextAlignmentRight;
    socContentLab.text = @"80%";
    socContentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    socContentLab.font = kBOLDSYSTEMFONT(14);
    [bgView addSubview:socContentLab];
    self.socContentLab = socContentLab;
    
    UIView *line2 = kView;
    line2.backgroundColor = kCOLOR_HEX(@"E7E7E7");
    [bgView addSubview:line2];
    
    UIView *line3 = kView;
    line3.backgroundColor = kCOLOR_HEX(@"E7E7E7");
    [bgView addSubview:line3];
    
    QMUIButton *pcsBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [pcsBtn setTitle:@"PCS" forState:UIControlStateNormal];
    [pcsBtn setTitleColor:kCOLOR_HEX(@"3F4443") forState:UIControlStateNormal];
    pcsBtn.titleLabel.font = kSYSTEMFONT(14);
    [bgView addSubview:pcsBtn];
    
    [[pcsBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.pcsBlock){
            weakSelf.pcsBlock();
        }
    }];
    
    QMUIButton *clusterBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [clusterBtn setTitle:@"电池族".rh_localized forState:UIControlStateNormal];
    [clusterBtn setTitleColor:kCOLOR_HEX(@"3F4443") forState:UIControlStateNormal];
    clusterBtn.titleLabel.font = kSYSTEMFONT(14);
    [bgView addSubview:clusterBtn];
    
    [[clusterBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.clusterBlock){
            weakSelf.clusterBlock();
        }
    }];
    

    [blockImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(15);
        make.size.equalTo(@(CGSizeMake(20, 20)));
    }];
    
    [blockTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blockImg.mas_right).offset(10);
        make.centerY.equalTo(blockImg);
    }];
    
    [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(blockImg);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blockImg);
        make.top.equalTo(blockImg.mas_bottom).offset(10);
        make.right.equalTo(detailBtn);
        make.height.offset(1);
    }];
    
    [statusTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line);
        make.top.equalTo(line.mas_bottom).offset(20);
    }];
    
    [statusContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line);
        make.centerY.equalTo(statusTitleLab);
    }];
    
    [powerTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(statusTitleLab);
        make.top.equalTo(statusTitleLab.mas_bottom).offset(15);
    }];
    
    [powerContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(statusContentLab);
        make.centerY.equalTo(powerTitleLab);
    }];
    
    [powerImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(20, 20)));
        make.right.equalTo(powerContentLab.mas_left).offset(-5);
        make.centerY.equalTo(powerContentLab);
    }];
    
    [socTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(powerTitleLab);
        make.top.equalTo(powerTitleLab.mas_bottom).offset(15);
    }];
    
    [socContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(powerContentLab);
        make.centerY.equalTo(socTitleLab);
    }];
    
    [socProgress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(socContentLab.mas_left).offset(-5);
        make.centerY.equalTo(socContentLab);
        make.width.offset(45);
        make.height.offset(6);
    }];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(socTitleLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    [pcsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(line2.mas_bottom);
        make.width.equalTo(bgView).multipliedBy(0.5);
        make.height.offset(kScaleW(45));
    }];
    
    [clusterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pcsBtn.mas_right);
        make.top.equalTo(pcsBtn);
        make.size.equalTo(pcsBtn);
        make.bottom.mas_offset(0);
    }];
    
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom);
        make.left.equalTo(pcsBtn.mas_right);
        make.width.offset(1);
        make.bottom.equalTo(pcsBtn);
    }];
}
@end
